<?php

/**
 * @file
 * 
 * @package    Europa
 * @subpackage View
 */

/**
 * @class
 * 
 * @name Europa_View
 * @desc Renders view scripts. Used to render both the layout and view in Europa.
 */
abstract class Europa_View extends Europa_Abstract
{
	public static $defaultConfig = array(
				'pluginPath'   => './app/plugins',
				'pluginPrefix' => '',
				'pluginSuffix' => 'Plugin'
	);
	
	/**
	 * 
	 */
	protected $_content = array();

	public function __construct($config = null, $args = array())
	{
		parent::__construct($config, self::$defaultConfig);
		
		// apply parameters
		foreach ((array) $args as $argName => $argValue) {
			$this->$argName = $argValue;
		}	
	}
	
	/**
	 * @method
	 * @magic
	 * 
	 * @name __call
	 * @desc Loads a plugin and executes it. Throws an exception if not found.
	 * 
	 * @return Mixed
	 */
	public function __call($func, $args)
	{
		$func  = ucfirst($func);
		$class = new Europa_String($this->getConfig('pluginPrefix') . $func . $this->getConfig('pluginSuffix'));
		$class = (string) $class;
		
		// if the class can't be loaded, attempt to load from the default europa directory
		// and throw an exception if it can't be found
		if (!Europa_Loader::loadClass($class, $this->getConfig('pluginPath'))) {
			$class = $fallback;
		} else {
			Europa_Exception::trigger('Cannot find plugin <strong>' . $func . '</strong>.');
		}
		
		// istantiate the plugin
		$class = new $class();
		
		// then call the plugin method if it exists
		if (method_exists($class, $func)) {
			return call_user_func_array(array($class, $func), $args);
		}
		
		return $class;
	}
	
	/**
	 * @method
	 * @public
	 * 
	 * @name uri
	 * @desc Takes a request uri and turns it into a full URI, without the server hostname part.
	 * 
	 * @param string $uri - The request URI to transform
	 * 
	 * @return string
	 */
	public function uri($uri = null)
	{
		// if a uri is passed, format it
		if ($uri) {
			$uri = trim($uri, '/') . '/';
		}
		
		// automate the root uri
		return '/' . Europa_Controller::getActiveInstance()->rootUri . '/' . $uri;
	}	
		
	/**
	 * Use the magic getter to set view variables
	 * @method
	 * @magic
	 * @param string $name
	 * @return The view variable
	 */
	public function __get($name)
	{
		return $this->_content[$name];	
	}
	
	/**
	 * Use the magice setter for view variables
	 * @method
	 * @magic 
	 * @param unknown_type $name The variable name
	 * @param unknown_type $value The variable value
	 * @return boolean True, if it could be set
	 */
	public function __set($name, $value)
	{
		$this->_content[$name] = $value;
	}
	
	/**
	 * @method
	 * @magic
	 * 
	 * @name __toString
	 * @desc Returns the contents of the rendered view.
	 * 
	 * @return String
	 */
	public abstract function __toString();
	
	/**
	 * @method
	 * @public
	 * 
	 * @name render
	 * @desc Sets the script to be rendered.
	 * 
	 * @param String $script - The path to the script to be rendered relative to the view path, excluding the extension.
	 * 
	 * @return Object Europa_View
	 */
	public abstract function render($script);	
}
